package com.wwwarehouse.common.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.tools.LogUtils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Response;


public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;
//    LoaddingView loaddingView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
//        loaddingView = (LoaddingView) findViewById(R.id.loadding_view);
        //微信的配置应该写到constant里面啊。。。
        api = WXAPIFactory.createWXAPI(this, "wxb716f229c6313a0e");
        api.handleIntent(getIntent(), this);
        Intent intent = getIntent();
        if (intent != null) {
            if ("GS".equals(intent.getStringExtra("registerType"))) {
                paySuccess();
            }
        }
//        api = WXAPIFactory.createWXAPI(this, "wxb716f229c6313a0e");
//        api.handleIntent(getIntent(), this);
//        Intent intent = getIntent();
//        if (intent != null) {
//            if ("GS".equals(intent.getStringExtra("registerType"))) {
//                paySuccess();
//            }
//        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        ///第四步
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
//        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle(getResources().getString(R.string.app_tip));
//            if (resp.errCode == ERR_OK) {
//                switch (Common.getInstance().wxPayType) {
//                    case "CREDIT":
//                        paySuccess();
//                        break;
//                    case "SUBMIT_MONEY":
//                        /*if (Common.getInstance().isReceiveTask()) {
//                            EventBus.getDefault().post(new FinishEvent("finishPayCredit"));
//                            finish();
//                        } else {
//                            startActivity(CardDeskActivity.class, "data", "finish", true);
//                        }*/
//                        paySuccess();
//                        break;
//                }
//
//                return;
//            }
//            else if (resp.errCode == -1) {
//                loaddingView.setLoadding(false);
//                loaddingView.setVisibility(View.GONE);
//                builder.setMessage(getResources().getString(R.string.app_fail_pay) + resp.errCode);
//                builder.setNegativeButton(getResources().getString(R.string.app_return), new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        finish();
//                        api.unregisterApp();
//                        api.detach();
//                        api.handleIntent(getIntent(), null);
//                    }
//                });
//            } else if (resp.errCode == -2) {
//                loaddingView.setLoadding(false);
//                loaddingView.setVisibility(View.GONE);
//                builder.setMessage(getResources().getString(R.string.app_drop_pay) + resp.errCode);
//                builder.setNegativeButton(getResources().getString(R.string.app_return), new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        finish();
//                        api.unregisterApp();
//                        api.detach();
//                        api.handleIntent(getIntent(), null);
//                    }
//                });
//            }
//
//            AlertDialog alertDialog = builder.create();
//
//            alertDialog.setCanceledOnTouchOutside(false);//设置触摸对话框的外侧区域，不取消对话框
//            alertDialog.show();
//            //有dialog的情况下，返回键的监听
//            alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
//                @Override
//                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                    if (keyCode == KeyEvent.KEYCODE_BACK) {
//                        finish();
//                    }
//                    return false;
//                }
//            }
//            );
        }
//            });
//        }
    }

    OnResponseListener<String> onResponseListener = new OnResponseListener<String>() {
        @Override
        public void onStart(int what) {

        }

        @Override
        public void onSucceed(int what, Response<String> response) {
            LogUtils.showLog(response.get());
            try {

//            try {
//                ActiveEntryOrStoreHouseRes activeEntryOrStoreHouseRes = JSON.parseObject(response.get(), ActiveEntryOrStoreHouseRes.class);
//                if ("0".equals(activeEntryOrStoreHouseRes.code)) {
//                    if (Common.getInstance().isReceiveTask()) {
//                        EventBus.getDefault().post(new FinishEvent("finishPayCredit"));
//                        finish();
//                    } else {
//                        startActivity(CardDeskActivity.class, "data", "finish", true);
//                    }
//                } else if ("4".equals(activeEntryOrStoreHouseRes.code)) {
//                    Common.getInstance().logOut(WXPayEntryActivity.this);
//                }
            } catch (Exception e) {
                LogUtils.showInfoLog(e.toString());
            }
//            } catch (Exception e) {
//                LogUtils.showInfoLog(e.toString());
//            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {
//            toast(response.get() + "网络请求进入onFailed方法");
        }

        @Override
        public void onFinish(int what) {
//            loaddingView.setLoadding(false);
//            loaddingView.setVisibility(View.GONE);
        }
    };

    private void paySuccess() {

//        HashMap map = new HashMap();
//        map.put("outTradeNo", PayUtil.WXoutTradeNo);
//        map.put("cardUkid", PayUtil.WXcardUkid);
//        map.put("prepayId", PayUtil.WXprepayId);
//        map.put("tsOperationUkid", PayUtil.WXtsOperationUkid);
//        map.put("registerType", PayUtil.WXregisterType);
//        //                map.put("partnerId", payCreditFragment.WXpartnerId);
//        LogUtils.showLog("outTradeNo    " + PayUtil.WXoutTradeNo + "cardUkid       " + PayUtil.WXcardUkid + "prepayId        " +
//                PayUtil.WXprepayId + "tsOperationUkid          " + PayUtil.WXtsOperationUkid);
//        Request<String> request = NoHttpUtils.getRequest(Constant.WXCHECK, map);
//        requestQueue.add(0, request, onResponseListener);
    }
}